package com.smartrodin.springbootrabbitmq.service;

import com.rabbitmq.client.Channel;
import com.smartrodin.springbootrabbitmq.util.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author: arnold_nie
 * @date: 2020/7/25 11:04
 * FileName: ReceiveMessageService
 * Description: 接受消息 service
 */
@Slf4j
@Service
public class ReceiveMessageService {

    @RabbitListener(queues = {Constant.QUEUE_PUSH_ORDER_PAY_SUCCESS})
    public void getMessageDirect1(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        try {
            byte[] body = message.getBody();
            String msg = new String( body );
            log.info( "接收到 Direct 类型消息: {}，队列：{}", msg, Constant.QUEUE_PUSH_ORDER_PAY_SUCCESS );
            channel.basicAck( tag, false );
        } catch (IOException e) {
            log.error( "getMessageDirect1  handleMessage {} , error: {}", message, e );
            // 处理消息失败，将消息重新放回队列
            channel.basicNack( message.getMessageProperties().getDeliveryTag(), false, true );
        }
    }

    @RabbitListener(queues = {Constant.QUEUE_PUSH_USER_REGISTER})
    public void getMessageDirect2(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        try {
            byte[] body = message.getBody();
            String msg = new String( body );
            log.info( "接收到 Direct 类型消息: {}，队列：{}", msg, Constant.QUEUE_PUSH_USER_REGISTER );
            channel.basicAck( tag, false );
        } catch (IOException e) {
            log.error( "getMessageDirect2  handleMessage {} , error: {}", message, e );
            // 处理消息失败，将消息重新放回队列
            channel.basicNack( message.getMessageProperties().getDeliveryTag(), false, true );
        }
    }

    @RabbitListener(queues = {Constant.QUEUE_PUSH_INTEGRAL})
    public void getMessageFanout1(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        try {
            byte[] body = message.getBody();
            String msg = new String( body );
            log.info( "接收到 Fanout 类型消息: {}， 队列：{}", msg, Constant.QUEUE_PUSH_INTEGRAL );
            channel.basicAck( tag, false );
        } catch (IOException e) {
            log.error( "getMessageFanout1  handleMessage {} , error: {}", message, e );
            // 处理消息失败，将消息重新放回队列
            channel.basicNack( message.getMessageProperties().getDeliveryTag(), false, true );
        }
    }

    @RabbitListener(queues = {Constant.QUEUE_PUSH_COUPON})
    public void getMessageFanout2(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        try {
            byte[] body = message.getBody();
            String msg = new String( body );
            log.info( "接收到 Fanout 类型消息: {}， 队列：{}", msg, Constant.QUEUE_PUSH_COUPON );
            channel.basicAck( tag, false );
        } catch (IOException e) {
            log.error( "getMessageFanout2  handleMessage {} , error: {}", message, e );
            // 处理消息失败，将消息重新放回队列
            channel.basicNack( message.getMessageProperties().getDeliveryTag(), false, true );
        }
    }

    @RabbitListener(queues = {Constant.QUEUE_PUSH_ORDER_PAY_STATUS})
    public void getMessageTopic1(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        try {
            byte[] body = message.getBody();
            String msg = new String( body );
            log.info( "接收到 Topic 类型消息: {}， 队列：{}", msg, Constant.QUEUE_PUSH_ORDER_PAY_STATUS );
            channel.basicAck( tag, false );
        } catch (IOException e) {
            log.error( "getMessageTopic1  handleMessage {} , error: {}", message, e );
            // 处理消息失败，将消息重新放回队列
            channel.basicNack( message.getMessageProperties().getDeliveryTag(), false, true );
        }
    }

    @RabbitListener(queues = {Constant.QUEUE_PUSH_ORDER_SINGLE_DISCOUNT})
    public void getMessageTopic2(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        try {
            byte[] body = message.getBody();
            String msg = new String( body );
            log.info( "接收到 Topic 类型消息: {}， 队列：{}", msg, Constant.QUEUE_PUSH_ORDER_SINGLE_DISCOUNT );
            channel.basicAck( tag, false );
        } catch (IOException e) {
            log.error( "getMessageTopic2  handleMessage {} , error: {}", message, e );
            // 处理消息失败，将消息重新放回队列
            channel.basicNack( message.getMessageProperties().getDeliveryTag(), false, true );
        }
    }
}
